package com.ww.springboot.dd.milvus;

import com.ww.springboot.boot.utils.json.JsonUtil;
import io.milvus.param.collection.GetCollectionStatisticsParam;
import io.milvus.response.GetCollStatResponseWrapper;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.collection.request.DescribeCollectionReq;
import io.milvus.v2.service.collection.request.GetCollectionStatsReq;
import io.milvus.v2.service.collection.response.DescribeCollectionResp;
import io.milvus.v2.service.collection.response.GetCollectionStatsResp;
import io.milvus.v2.service.vector.request.DeleteReq;
import io.milvus.v2.service.vector.request.GetReq;
import io.milvus.v2.service.vector.request.SearchReq;
import io.milvus.v2.service.vector.request.data.BaseVector;
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.response.GetResp;
import io.milvus.v2.service.vector.response.SearchResp;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 描述：
 *
 * @author 🧑 ‍wanwei
 * @since 2024-11-22 09:08
 */
public class CreateDemo {

    public static void main(String[] args) throws InterruptedException {
        //System.out.println(Arrays.asList(Arrays.asList("454100655207391618")));

//        // 1. Connect to Milvus server
//        ConnectParam connectParam = ConnectParam.newBuilder()
//                .withUri("http://10.50.9.18:19530")
//                //.withToken(TOKEN)
//                .build();
//
//        MilvusServiceClient client = new MilvusServiceClient(connectParam);

        ConnectConfig connectConfig = ConnectConfig.builder().uri("http://10.50.9.18:19530").build();
        MilvusClientV2 milvusClientV2 = new MilvusClientV2(connectConfig);
//        System.out.println(milvusClientV2.listCollections());
//        System.out.println(milvusClientV2.listDatabases());
        //ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder().collectionName("image_search_wanwei").build();
        //System.out.println(milvusClientV2.listPartitions(listPartitionsReq));
//        DescribeCollectionReq d = DescribeCollectionReq.builder().collectionName("image_search_wanwei").build();
//        DescribeCollectionResp describeCollectionResp = milvusClientV2.describeCollection(d);
//        System.out.println(describeCollectionResp);
        int i = 0;
        GetCollectionStatsReq getCollectionStatsReq = GetCollectionStatsReq.builder().collectionName("image_search_wanwei").build();

        while (i <5000){
            // 获取集合的统计信息
            GetCollectionStatsResp collectionStats = milvusClientV2.getCollectionStats(getCollectionStatsReq);
            System.out.println("集合中的总条数: " + JsonUtil.toJson(collectionStats));
            Thread.sleep(20000);
        }

//        List<BaseVector> list = new ArrayList<>();
//        List<Float> floats = new ArrayList<>();
//        for (int i = 0; i < 2048; i++) {
//            floats.add(Float.valueOf(String.valueOf(i)));
//        }
//        FloatVec floatVec = new FloatVec(floats);
//        list.add(floatVec);
//        SearchReq searchReq = SearchReq.builder()
//                .collectionName("image_search_wanwei")
//                .limit(1)
//                .data(list)
//                .topK(1)
//                .build();
//        SearchResp search = milvusClientV2.search(searchReq);
//        System.out.println(search.getSearchResults());
//
//
//        GetReq getReq = GetReq.builder()
//                .collectionName("image_search_wanwei")
//                        .ids(Arrays.asList(454100655207391618L)).build();
//        GetResp getResp = milvusClientV2.get(getReq);
//        System.out.println(getResp);
//
//
//        DeleteReq deleteReq = DeleteReq.builder().collectionName("image_search_wanwei").ids(Arrays.asList(454100655207391618L)).build();
//        milvusClientV2.delete(deleteReq);


//        R<ListDatabasesResponse> listDatabasesResponse = client.listDatabases();
//        //System.out.println(JsonUtil.toJson(listDatabasesResponse.getData()));
//        System.out.println(listDatabasesResponse.getData());
//
//        ListCollectionsParam listCollectionsParam = ListCollectionsParam.newBuilder().build();
//        R<ListCollectionsResponse> listCollectionsResponseR = client.listCollections(listCollectionsParam);
//        System.out.println(listCollectionsResponseR.getData());
//
//        DescribeCollectionParam describeCollectionParam = DescribeCollectionParam.newBuilder()
//                .withCollectionName("test_bullfrog_image")
//                .withDatabaseName("default")
//                .build();
//
//        R<DescribeCollectionResponse> describeCollectionResponseR = client.describeCollection(describeCollectionParam);
//
//        System.out.println(describeCollectionResponseR.getData());
//        LoadCollectionParam collectionParam = LoadCollectionParam
//                .newBuilder()
//                .withCollectionName("test_bullfrog_image")
//                .build();
//
//        R<RpcStatus> rpcStatusR = client.loadCollection(collectionParam);
//        System.out.println(rpcStatusR.getData());
//
//        ShowPartitionsParam partitionsParam = ShowPartitionsParam.newBuilder().withCollectionName("test_bullfrog_image").build();
//        R<ShowPartitionsResponse> showPartitionsResponseR = client.showPartitions(partitionsParam);
//
//        System.out.println(showPartitionsResponseR);
//
//
//        // 4. Single vector search
//        //List<List<Float>> query_vectors = Arrays.asList();
//        //List<BaseVector> query_vector_list = query_vectors.stream().map(v -> new FloatVec(v)).collect(Collectors.toList());
//
//        byte[] bytes = HttpRequest.get("https://bullfrog-master.oss-cn-shenzhen.aliyuncs.com/delivery_label/20710061711015.pdf").execute().bodyBytes();
//
//        List<Float> list = new ArrayList<>();
//        for (int i = 0; i < 2048; i++) {
//            list.add(Float.valueOf(String.valueOf(bytes[i])));
//        }
//
//        List<List<Float>> query_vectors = Arrays.asList(list);
//        SearchSimpleParam searchSimpleParam = SearchSimpleParam.newBuilder()
//                .withCollectionName("test_bullfrog_image")
//                .withVectors(query_vectors)
//                .withLimit(3L)
//                .build();
//
//        R<SearchResponse> search = client.search(searchSimpleParam);
//
//        System.out.println(search.getData().rowRecords);
//
//
//        GetIdsParam getIdsParam = GetIdsParam.newBuilder()
//                .withCollectionName("test_bullfrog_image")
//                .withPrimaryIds(Arrays.asList("454057352823739418"))
//                .build();
//        R<GetResponse> getResponseR = client.get(getIdsParam);
//        System.out.println(getResponseR.getData());
//
//        client.

    }
}
